<template>
  <em :style="styleObj" class="uni-icons" :class="['uniui-' + type, customPrefix, customPrefix ? type : '']" @click="_onClick">
    <slot></slot>
  </em>
</template>
<script>
import { fontData } from './uniicons_file_vue.js'

const getVal = (val) => {
  const reg = /^[0-9]*$/g
  return typeof val === 'number' || reg.test(val) ? val + 'px' : val
}
export default {
  name: 'UniIcons',
  props: {
    type: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: '#333333'
    },
    size: {
      type: [Number, String],
      default: 16
    },
    customPrefix: {
      type: String,
      default: ''
    },
    fontFamily: {
      type: String,
      default: ''
    }
  },
  emits: ['click'],
  data() {
    return {
      icons: fontData
    }
  },
  computed: {
    unicode() {
      const code = this.icons.find((v) => v.font_class === this.type)
      if (code) {
        return code.unicode
      }
      return ''
    },
    iconSize() {
      return getVal(this.size)
    },
    styleObj() {
      if (this.fontFamily !== '') {
        return `color: ${this.color}; font-size: ${this.iconSize}; font-family: ${this.fontFamily};`
      }
      return `color: ${this.color}; font-size: ${this.iconSize};`
    }
  },
  methods: {
    _onClick() {
      this.$emit('click')
    }
  }
}
</script>

<style scoped>
@import './uniicons.css';

@font-face {
  font-family: uniicons;
  src: url('./uniicons.ttf');
}
.uni-icons {
  font-family: uniicons;
  text-decoration: none;
  text-align: center;
}
</style>
